import { Body, Controller, Delete, Get, Param, Post, Query, Request, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { SessionService } from './session.service';
import { CreateSessionDto } from './dto/create-session.dto';

@ApiTags('Session')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('sessions')
export class SessionController {
  constructor(private readonly sessionService: SessionService) {}

  // Create session
  @Post()
  @ApiOperation({ summary: 'Create session' })
  @ApiResponse({ status: 201, description: 'Created successfully' })
  @ApiBody({ type: CreateSessionDto })
  async create(@Request() req, @Body() body: CreateSessionDto) {
    const userId = req.user.id;
    return this.sessionService.create(userId, body.personaId, body.skill);
  }

  // Get session details
  @Get(':id')
  @ApiOperation({ summary: 'Get session details (current user)' })
  async get(@Request() req, @Param('id') id: string) {
    const userId = req.user.id;
    return this.sessionService.getByIdForUser(userId, id);
  }

  // Get session list
  @Get()
  @ApiOperation({ summary: 'Get current user session list (paginated)' })
  async list(
    @Request() req,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('order') order?: 'asc' | 'desc',
  ) {
    const userId = req.user.id;
    return this.sessionService.listForUser(userId, { page: Number(page), pageSize: Number(pageSize), order });
  }

  // Delete session
  @Delete(':id')
  @ApiOperation({ summary: 'Delete session and all messages' })
  @ApiResponse({ status: 200, description: 'Deleted successfully' })
  @ApiResponse({ status: 404, description: 'Session not found' })
  @ApiResponse({ status: 403, description: 'No permission to delete this session' })
  async delete(@Request() req, @Param('id') id: string) {
    const userId = req.user.id;
    return this.sessionService.deleteForUser(userId, id);
  }
}